<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__dict">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-button type="primary" @click="getClassfy()">分类管理</el-button>
          <el-button v-if="$hasPermission('hotel:otherexpenseproject:save')" type="primary" @click="addOrUpdateHandle()"
            >新增项目</el-button
          >
        </el-form-item>
        <!-- <el-form-item>
          <el-button
            type="danger"
            @click="deleteHandle(id)"
            >{{ $t("deleteBatch") }}</el-button
          >
        </el-form-item> -->
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="categoryName"
          label="分类名称"
          header-align="center"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="projectName"
          label="项目名称"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="unit"
          label="单位"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="defaultPrice"
          label="默认价格"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="createDate"
          label="创建时间"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          :label="$t('handle')"
          fixed="right"
          header-align="center"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
             v-if="$hasPermission('hotel:otherexpenseproject:update')"
              type="text"
              size="small"
              @click="addOrUpdateHandle(scope.row.id)"
              >{{ $t("update") }}</el-button
            >
            <el-button
             v-if="$hasPermission('hotel:otherexpenseproject:delete')"
              type="text"
              size="small"
              @click="deleteHandle(scope.row.id)"
              >{{ $t("delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle"
      >
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        @refreshDataList="getDataList"
      ></add-or-update>
    </div>
    <el-dialog
      :visible.sync="visible"
      title="分类管理"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="50%"
    >
      <el-button
        v-if="$hasPermission('hotel:otherexpensecategory:save')"
        type="primary"
        @click="addClassfy()"
        style="margin-bottom: 20px"
        >新增项目</el-button
      >
      <el-table
        v-loading="dataListLoading"
        :data="dataList2"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="categoryName"
          label="分类名称"
          header-align="center"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="createDate"
          label="创建时间"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          :label="$t('handle')"
          fixed="right"
          header-align="center"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              v-if="$hasPermission('hotel:otherexpensecategory:delete')"
              type="text"
              size="small"
              @click="deleteHandle2(scope.row.id)"
              >{{ $t("delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./other-add-update";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/hotel/other/expense/project/page",
        getDataListIsPage: true,
        deleteURL: "/hotel/other/expense/project",
        deleteIsBatch: true,
      },
      dataList2: [],
      visible: false,
      dataForm: {},
      dataForm2: {
        homestayId: this.$store.state.user.homestayId,
        page: 1,
        limit: 100,
      },
      dialogFormVisible: false,
    };
  },
  components: {
    AddOrUpdate,
  },
  created() {},
  methods: {
    getClassfy() {
      this.visible = true;
      this.$http
        .get("/hotel/other/expense/category/page", { params: this.dataForm2 })
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataList2 = res.data.data.list;
        });
    },
    // 删除
    deleteHandle2(id) {
      this.$confirm(
        this.$t("prompt.info", { handle: this.$t("delete") }),
        this.$t("prompt.title"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          type: "warning",
        }
      )
        .then(() => {
          this.$http
            .delete(`/hotel/other/expense/category`, {
              data: [id],
            })
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.getClassfy();
                },
              });
            })
            .catch(() => {});
        })
        .catch(() => {});
    },
    addClassfy(id) {
      this.$prompt("请输入分类名称", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          if (value) {
            this.$http[!id ? "post" : "put"]("/hotel/other/expense/category", {
              categoryName: value,
              homestayId: this.dataForm2.homestayId,
            }).then((res) => {
              if (res.data.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 500,
                onClose: () => {
                  this.getClassfy();
                },
              });
            });
          }
        })
        .catch(() => {});
    },
  },
};
</script>
